package struts.action.account;

import java.util.LinkedHashMap;
import java.util.List;

import com.opensymphony.xwork2.ActionContext;

import hibernate.dao.UserDAO;
import hibernate.model.object.User;
import spring.bean.UserService;

public class RegisterAction {
	private  String username;
	private  String password;
	private  String bod;
	private  String email;
	private  String vcode;
	
	private  int sexId;
	private  int cityId;
	
	private List<User> users;
	
	private  UserService userService;
	private  UserDAO userDAO;
	
	public String execute() throws Exception{
		User user = new User();
		user.setBod(bod);
		user.setEmail(email);
		user.setUsername(username);
		user.setPassword(password);
		user.setCityId(cityId);
		user.setSexId(sexId);
		System.out.println("username>>>>"+user.getUsername());
		String uName = userService.reg(user);
//		String result = userService.verifyUser(email, password);
		if(!"".equals(username) && null != username && username.equals(uName)){
			ActionContext.getContext().getSession().put("userDTO", user);
			return "loginOK";
		}else{
			return "error";
		}
	}
	public String login() throws Exception{
		System.out.println(">>>loing! processing!");
		if(null==email || "".equals(email))
			throw new Exception("email is null");
		if(null==password || "".equals(password))
			throw new Exception("password is null");
		
		String genVcode = (String)ActionContext.getContext().getSession().get("validationCode");
		if(!genVcode.equalsIgnoreCase(vcode))
			throw new Exception("input correct validation code.");
		
		User user = userService.verifyUser(email, password);
		if(null == user)
			throw new Exception("can not find this user.");
		else{
			ActionContext.getContext().getSession().put("userDTO", user);
			return "loginOK";
		}
	}
	
	public String findAllUser() throws Exception{
		String result = "error";
		LinkedHashMap<String, String> orderBy = new LinkedHashMap<String, String>();
		orderBy.put("id", "desc");
		users = userDAO.getResultList(User.class, "", orderBy);
		if(null != users && users.size() != 0)
			result = "findUsers";
		return result;
	}
	
	
	
	public String getBod() {
		return bod;
	}
	public void setBod(String bod) {
		this.bod = bod;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}



	public String getVcode() {
		return vcode;
	}
	public void setVcode(String vcode) {
		this.vcode = vcode;
	}
	public int getCityId() {
		return cityId;
	}



	public void setCityId(int cityId) {
		this.cityId = cityId;
	}



	public int getSexId() {
		return sexId;
	}



	public void setSexId(int sexId) {
		this.sexId = sexId;
	}

	

	public List<User> getUsers() {
		return users;
	}
	public void setUsers(List<User> users) {
		this.users = users;
	}
	public UserService getUserService() {
		return userService;
	}



	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	public UserDAO getUserDAO() {
		return userDAO;
	}
	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}
	
}
